var mousewheel = {}


mousewheel.init = function() {
	var map = document.getElementById('map');
	if (map.addEventListener) {
		map.addEventListener("mousewheel", this.handler, false);
		map.addEventListener("DOMMouseScroll", this.handler, false); //firefox
	}
}

mousewheel.handler = function(event) {
	// No support for old IE 
	var delta = event.wheelDelta || -event.detail; // detail firefox negative
	var sign = delta > 0 ? 1 : delta < 0 ? -1 : 0; //sign
	map_view.change_scale(sign);
	svg2.selectAll("circle")
	     .attr("transform", 
   		  translate_map(map_view.X, map_view.Y, map_view.get_scale()));
	svg2.selectAll("#mappath")
    .attr("transform", 
		  translate_map(map_view.X, map_view.Y, map_view.get_scale()));
	if(event.preventDefault) {
		event.preventDefault();
	}
	return false;
}

var mouseclick = {};

mouseclick.mode = "UP";

mouseclick.startX = null;
mouseclick.startY = null;

mouseclick.init = function() {
	var map = $("#map");
	map.mousedown(function(event){
		if (event.button == 0) {
				if (mouseclick.mode == "UP") {
				mouseclick.mode = "DOWN";
				mouseclick.startX = event.pageX;
				mouseclick.startY = event.pageY;
				if(event.preventDefault) {
					event.preventDefault();
				}
				return false;
			}
		}
	});
	var html = $("html");
	html.mousemove(function(event){
		if (event.button == 0) {
			if (mouseclick.mode == "DOWN") {
				var deltaX = event.pageX - mouseclick.startX;
				var deltaY = event.pageY - mouseclick.startY;
				svg2.selectAll("circle")
			     .attr("transform", 
		   		  translate_map(map_view.X + deltaX, map_view.Y + deltaY, map_view.get_scale()));
				svg2.selectAll("#mappath")
			    .attr("transform", 
				  translate_map(map_view.X + deltaX, map_view.Y + deltaY, map_view.get_scale()));
			}
			if(event.preventDefault) {
				event.preventDefault();
			}
			return false;
		}
	});
	html.mouseup(function(event){
		if (event.button == 0) {
			if (mouseclick.mode == "DOWN") {
				var deltaX = event.pageX - mouseclick.startX;
				var deltaY = event.pageY - mouseclick.startY;
				map_view.X += deltaX
				map_view.Y += deltaY
				svg2.selectAll("circle")
			     .attr("transform", 
		   		  translate_map(map_view.X, map_view.Y, map_view.get_scale()));
				svg2.selectAll("#mappath")
			    .attr("transform", 
				  translate_map(map_view.X, map_view.Y, map_view.get_scale()));
				mouseclick.mode = "UP";
			}
			if(event.preventDefault) {
				event.preventDefault();
			}
			return false;
		}
	});
}

